%% task6_2_1_1.m

clear;
% F调唱名对应频率
% 第一行小字组，第二行小字一组
fre_f = [174.61, 196, 220, 233.08, 261.63, 293.66, 329.63;
         349.23, 392, 440, 466.16, 523.25, 587.33, 659.25];

dfh1_1_1 = SingleSound6_2_1_1(fre_f(2,5), 8000, 10, 0.5);
dfh1_2_1 = SingleSound6_2_1_1(fre_f(2,5), 8000, 10, 0.25);
dfh1_2_2 = SingleSound6_2_1_1(fre_f(2,6), 8000, 10, 0.25);
dfh2_1_1 = SingleSound6_2_1_1(fre_f(2,2), 8000, 10, 1);
dfh3_1_1 = SingleSound6_2_1_1(fre_f(2,1), 8000, 10, 0.5);
dfh3_2_1 = SingleSound6_2_1_1(fre_f(2,1), 8000, 10, 0.25);
dfh3_2_2 = SingleSound6_2_1_1(fre_f(2,6), 8000, 10, 0.25);
dfh4_1_1 = dfh2_1_1;

dfh = [dfh1_1_1; dfh1_2_1; dfh1_2_2; dfh2_1_1; dfh3_1_1; dfh3_2_1; dfh3_2_2; dfh4_1_1];

sound(dfh, 8000);
plot(dfh);

%% SingleSound.m

% SingleSound: 单个乐音生成器
% fre: 乐音频率
% sampRate: 采样率
% amplitude: 乐音幅值（最大值）
% duration: 乐音时长
% res: 返回以正弦波为基础的幅值发生衰减的采样后的离散值
%      大小[时长*采样率 + 1， 1]
function res = SingleSound6_2_1_1(fre, sampRate, amplitude, duration)
    res = zeros(duration * sampRate + 1, 1);
    phi = 0;    % 初始相位
    turnPoint1 = 0.6;
    ind = 1;
    for t = 0:1/sampRate:duration
        if t <= turnPoint1 * duration
            res(ind,1) = amplitude * sin(fre * t + phi);
        else
            res(ind,1) = (-amplitude / (1 - turnPoint1) / duration * t + amplitude / (1 - turnPoint1)) * sin(fre * t + phi);
        end
        ind = ind + 1;
    end
end